package com.topc.oj.language;



import com.topc.base.utils.LogUtils;
import com.topc.oj.constant.OJConstant;
import com.topc.oj.enums.CodeStatusEnum;
import com.topc.oj.result.CodeCompileResult;
import com.topc.oj.result.CodeRunResult;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public interface LanguageCodeDo {

    CodeStatusEnum checkOutputStream(String s);

    String taskId();

    String fileSuffix();

    String className();

    default CodeCompileResult doCompile(String code){
        String dir = OJConstant.SAVE_FILE_PATH + taskId();

        File file = new File(dir);
        //LogUtils.getLog().info("文件夹创建中 任务id:{}",taskId());
        boolean mkdir = file.mkdir();
        if (!mkdir) {
           // LogUtils.getLog().error("文件夹创建失败 任务id:{}",taskId());
            return new CodeCompileResult().setCodeStatusEnum(CodeStatusEnum.UN_KNOW);
        }
        //LogUtils.getLog().info("文件夹创建成功 任务id:{}",taskId());
        String fileName = dir + "\\" + className() + fileSuffix();
       // LogUtils.getLog().info("写入文件中 任务id:{}",taskId());
        try (FileWriter writer = new FileWriter(fileName)) {
            writer.write(code);
        } catch (IOException e) {
           // LogUtils.getLog().error("代码写入失败 任务id:{}",taskId());
            return new CodeCompileResult().setCodeStatusEnum(CodeStatusEnum.UN_KNOW);
        }
        //LogUtils.getLog().info("代码写入成功 任务id:{}",taskId());
        LogUtils.printChar("*",20);
        //System.out.println("代码内容");
        //System.out.println(code);
        LogUtils.printChar("*",20);
       // LogUtils.getLog().info("进入编译阶段 任务id:{}",taskId());
        CodeCompileResult codeCompileResult = runCompile(code);
        return codeCompileResult;
    }

    CodeCompileResult runCompile(String code);

    default CodeRunResult doRun(String input, int timeLimit, int memoryLimit){
       // LogUtils.getLog().info("运行任务 任务id:{}",taskId());
        CodeRunResult codeRunResult = run(input, timeLimit, memoryLimit);
        //LogUtils.getLog().info("输出结果为 :{}",codeRunResult.getContentInfo());
        //LogUtils.getLog().info("运行结束 任务id:{}",taskId());
        return codeRunResult;
    }

    CodeRunResult run(String input, int timeLimit, int memoryLimit);

}
